import { ref } from 'vue';
import { getSubsetApi } from '@/api/subset_api.ts';
import { useSubsetStore } from '@/stores/subset';

export function useSubset() {
  const subsetStore = useSubsetStore();
  const subsetList = ref<any>([]);
  const getSubset = async (classify: number) => {
    try {
      const res = await getSubsetApi({ classify: classify, state: 1 });
      let subset = res.data.data;
      subsetStore.data = subset.list;
      subsetStore.count = subset.count;
      //赋值解构方法避免浅拷贝
      subsetList.value = [...subsetStore.data];
      // 如果没有分类，就不添加未分类
      if (subsetStore.exclude.id) {
        subsetList.value.unshift(subsetStore.exclude);
      }
      subsetList.value.unshift({
        id: -1,
        name: '全部',
        value: subsetStore.count,
      });
    } catch (error) {
      console.log(error);
    }
  };

  return {
    getSubset,
    subsetList,
  };
}
